/**
 * PropertyTreeNode.java
 * Idea
 * @version version 1.0
 *
 * @author     Danny Ayers
 * @created    04-Dec-2002
 *
 * Copyright (c) 2002 D.Ayers
 * All rights reserved.
 *
 * For license details see http://ideagraph.net/licenses
 */
package com.idea.rdftree;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.tree.TreeNode;

import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Statement;
import com.idea.common.rdf.RdfUtils;
import com.idea.resources.tree.TreeIcons;

public class StatementNode extends AbstractRdfTreeNode  implements RdfTreeNode {
    //	private Property property;
    private Statement statement;
    private List children;

    public StatementNode(RdfNodeMap nodeMap, Statement statement) {
    	super(nodeMap, statement);
        this.statement = statement;

        //	setTreeModel(treeModel);
    }

	public RDFNode getRdfNode(){
	//return (RDFNode)statement.asResource();	// ?? is ok? cast added - change from Jena
								  // 1.6
	    return (RDFNode)statement.getResource();
	}
	
	
	public Statement getStatement(){
		return statement;
	}
    /*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.TreeNode#getChildAt(int)
	 */
    public TreeNode getChildAt(int childIndex) {
        System.out.println("\nstate.getChildAt " + childIndex);
        System.out.println(".getChildAt = " +
            (TreeNode)getChildren().get(childIndex));

        return (TreeNode)getChildren().get(childIndex);
    }

    /*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.TreeNode#getChildCount()
	 */
    public int getChildCount() {
        return getChildren().size();
    }

    /*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.TreeNode#getParent()
	 */
    public TreeNode getParent() {
        return getNodeMap().getTreeNode(RdfUtils.getParent(getModel(),
                statement.getSubject()));
    }

    /*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.TreeNode#getIndex(TreeNode)
	 */
    public int getIndex(TreeNode node) {
        return getChildren().indexOf(node);
    }

    /*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.TreeNode#getAllowsChildren()
	 */
    public boolean getAllowsChildren() {
        return true;
    }

    /*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.TreeNode#isLeaf()
	 */
    public boolean isLeaf() {
        return getChildren().size()== 0;
    }

    /*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.TreeNode#children()
	 */
    public Enumeration children() {
        return (new Vector(getChildren())).elements();
    }

    private List getChildren() {
        if((children == null) || isDirty()) {
            children = new ArrayList();

            try {
                /*
				 * NodeIterator iterator =
				 * nodeMap.getModel().listObjectsOfProperty(
				 * statement.getPredicate()); while (iterator.hasNext()) {
				 * children.add( nodeMap.getTreeNode((RDFNode)
				 * iterator.next())); }
				 */
                children.add(getNodeMap().getTreeNode(statement.getObject()));
            } catch(Exception exception) {
                exception.printStackTrace();
            }
        }

        return children;
    }

    public String toString() {
        return statement.getPredicate().getLocalName();
    }

    public ImageIcon getIcon() {
        return TreeIcons.propertyIcon;
    }

	public int getNodeType() {
		return RdfTreeNode.STATEMENT;
	}
	
	public void delete() {
	}

	public void rename(String newName) {
	}
}
